<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>
            Sqlite Trigger Generator
        </title>
        <style type="text/css">
        <!--
            @import "c/defaults.css";
            @import "c/typography.css";
            
            #results {
                font-size: 1.5em;
            }
        -->
        </style>
    </head>
    <body>
        <h1 class="title">
            SQLite Foreign Key Trigger Generator</h1>
        <? if( $show_results ) { ?>
            <h2> Your Results </h2>
            <div id="results">
                <?=$result_sql?>
            </div>
        <? } else {?> 
        <h2 class="title">
            Introduction</h2>
        <p> This generator is a work in progress.  The reason it exists is because
            <a href="http://www.sqlite.org">SQLite</a> parses fk column constraints but does not enforce them.
            <a href="http://www.sqlite.org/cvstrac/wiki?p=ForeignKeyTriggers"> You can read more about</a> the problem and its solution if you are interested.  The gist of it all is that triggers can be used in SQLite to enforce fk column constraints.  However, generating the triggers can sometimes be a pain. This generator will parse out your column definitions in a CREATE TABLE statement and generate the SQL to create the triggers needed to enforce the fk column constraints.
        </p> 
        <p> Please not this has not been tested very much!  Please report any bugs
            using the 
            <a href="http://rcs-comp.com/site/index.php/view/Contact_Us-Main">contact us</a>
            form.
        </p> 
        <h2 class="title">
            Supported ON UPDATE and ON DELETE actions</h2>
        <ul>
            <li>ON DELETE RESTRICT [default if not explicit]</li>
            <li>ON DELETE CASCADE</li>
        </ul>
        <p> Other actions can probably be implimented relatively easily if needed.  Send
            and email through the 
            <a href="http://rcs-comp.com/site/index.php/view/Contact_Us-Main">contact us</a>
            form if you have a feature request or bug report.
        </p>  
        <script type="text/javascript">
        //<![CDATA[
        function validate_SqliteTriggerForm(frm) {
        var value = '';
        var errFlag = new Array();
        _qfMsg = '';
        value = frm.elements['statement'].value;
        if (value == '' && !errFlag['statement']) {
        errFlag['statement'] = true;
        _qfMsg = _qfMsg + '\n - Create Statement is required';
        }
        if (_qfMsg != '') {
        _qfMsg = 'Invalid information entered.' + _qfMsg;
        _qfMsg = _qfMsg + '\nPlease correct these fields.';
        alert(_qfMsg);
        return false;
        }
        return true;
        }
        //]]>
        </script>
    <form class="dynamicForm" action="" method="post" name="SqliteTriggerForm" id="SqliteTriggerForm" onsubmit="return validate_SqliteTriggerForm(this);">
        <div id="SqliteTriggerForm--wrapper">
            <h2 id="SqliteTriggerForm--header"> Enter your SQLite create statement (with constraint) below:</h2>    
            <div class="rowOddFirst" id="SqliteTriggerForm-statement-row"> 
                <!--  -->
                <div class="form-required">
                    *
                </div> 
                <!--  -->
                <div class="row-label-wrapper">
                    <!--  -->
                    <label for="SqliteTriggerForm_elementId_statement" class="row-label"> Create Statement:
                    </label>
                </div> 
                <!--  -->
                <!--  -->
                <div class="row-element">
                    <textarea cols="75" rows="4" name="statement" id="SqliteTriggerForm_elementId_statement">  &#010;CREATE TABLE foo (&#010;    id INTEGER NOT NULL PRIMARY KEY&#010;);&#010;&#010;CREATE TABLE bar (&#010;    id INTEGER NOT NULL PRIMARY KEY,&#010;    fooId INTEGER&#010;           CONSTRAINT fk_foo_id REFERENCES foo(id),&#010;    fooId2 INTEGER NOT NULL&#010;           CONSTRAINT fk_foo_id2 REFERENCES foo(id) ON DELETE CASCADE&#010;);&#010;</textarea>
                </div> 
                <!--  -->
            </div>  
            <div class="rowEven" id="SqliteTriggerForm-inc_delete-row"> 
                <!--  -->
                <div class="form-required">
                    &nbsp;
                </div> 
                <!--  -->
                <div class="row-label-wrapper">
                    <!--  -->
                    <label for="qf_2d30b1" class="row-label"> Inc. Drop Statements:
                    </label>
                </div> 
                <!--  -->
                <!--  -->
                <div class="row-element">
                    <input name="inc_delete" type="checkbox" value="1" id="qf_2d30b1" />
                    <label for="qf_2d30b1">
                        (before each trigger)
                    </label>
                </div> 
                <!--  -->
            </div>  
            <div class="rowOdd" id="SqliteTriggerForm-inc_create-row"> 
                <!--  -->
                <div class="form-required">
                    &nbsp;
                </div> 
                <!--  -->
                <div class="row-label-wrapper">
                    <!--  -->
                    <label for="qf_8469f5" class="row-label"> Inc. Input SQL:
                    </label>
                </div> 
                <!--  -->
                <!--  -->
                <div class="row-element">
                    <input name="inc_create" type="checkbox" value="1" id="qf_8469f5" />
                    <label for="qf_8469f5">
                        (i.e. the create statements above)
                    </label>
                </div> 
                <!--  -->
            </div>  
            <div class="rowEven">
                <div class="form-required">
                    &nbsp;
                </div>
                <div class="row-label-wrapper" > &nbsp;
                </div>
                <div class="row-element">
                    <input class="button" name="btnSubmit" value="Submit" type="submit" id="SqliteTriggerForm_elementId_btnSubmit" />
                </div>
            </div>
        </div>
    </form>
    <p>
        <span class="requiredFieldsStar">
            *
        </span>
        indicates a required field
    </p>
    <? } ?>
    
    <h2>License</h2>
    <p>
        <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">
        <img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" />
        </a>
        <br />This work is licensed under a 
        <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0 License</a>.
    </p>
    
    <h2>For More Information</h2>
    <p><a href="http://rcs-comp.com/site/index.php/view/Utilities-SQLite_foreign_key_trigger_generator">http://rcs-comp.com/site/index.php/view/Utilities-SQLite_foreign_key_trigger_generator</a></p>
</body>
</html>
